package com.wuwei.elearning.config;

import io.minio.MinioClient;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;

@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {

    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;


    @Bean
    public MinioClient minioClient() {
        try {
            // 确保 endpoint 不以斜杠结尾
            String endpointUrl = endpoint.endsWith("/") ?
                    endpoint.substring(0, endpoint.length() - 1) : endpoint;

            MinioClient client = MinioClient.builder()
                    .endpoint(endpointUrl)
                    .credentials(accessKey, secretKey)
                    .build();

            // 测试连接
            client.listBuckets();
            return client;
        } catch (Exception e) {
            throw new RuntimeException("Error initializing MinIO client: " + e.getMessage(), e);
        }
    }


}
